﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using You2V.Data.Shared;
using You2V.LocalDBLayer;

namespace You2VTool.Controls
{
    /// <summary>
    /// Interaction logic for SponsorMain.xaml
    /// </summary>
    public partial class SponsorMain : UserControl
    {
        private List<Student_vs_Sponsor> Student_vs_SponsorList;

        public SponsorMain()
        {
            InitializeComponent();

            RefreshList();
        }

        private void RefreshList()
        {
            dg_Sponsor.ItemsSource = null;

            StudentSponsorDBL StudentSponsorDBL = new StudentSponsorDBL();
            Student_vs_SponsorList = StudentSponsorDBL.GetStudent_vs_SponsorGivenWhereCondition();

            dg_Sponsor.ItemsSource = Student_vs_SponsorList;


        }

        private void TriggerSearch()
        {
            string keyWord = this.SearchText.Text;

            if (string.IsNullOrWhiteSpace(keyWord))
            {
                RefreshList();
                return;
            }

            StudentSponsorDBL StudentSponsorDBL = new StudentSponsorDBL();
            Student_vs_SponsorList = StudentSponsorDBL.GetStudent_vs_SponsorByKeyword(keyWord);

            dg_Sponsor.ItemsSource = Student_vs_SponsorList;
        }


        private void SearchButton_Click(object sender, RoutedEventArgs e)
        {
            TriggerSearch();
        }

        private void CancelSearchButton_Click(object sender, RoutedEventArgs e)
        {
            this.SearchText.Text = string.Empty;
            RefreshList();
        }

        private void SearchText_KeyDown(object sender, KeyEventArgs e)
        {
            // 因为性能差，敲回车才执行
            if (e.Key == Key.Enter)
            {
                TriggerSearch();
            }
        }
    }
}
